package modellayer;

/**
 * This demo-class only shows the calculation of the Total made
 * from subtotals and different discounts from each <code>Item</code> and
 * different discounts from the <code>Person</code> (Customer, that is).
 * 
 * @author (ucn-1semprojekt-dm78-gruppe1)
 *
 */
public class Order
{
  private Person person;
  private OrderLines orderLines;
  
  /**
   * Makes the final calculation of the total. All discounted totals and
   * subtotals from each <code>Item</code> and <code>Person</code> are compared
   * with the non-discounted total reduced by the max discount percent
   * <code>maxDiscountPercent</code>
   * 
   * @param maxDiscountPercent  Despite all sorts of discounts, this ensures
   * a total with a discount no higher than this max discount percent.
   * @return the total
   */
  public double getTotal(double maxDiscountPercent)
  {
  	double total = orderLines.getTotal();
  	double maxDiscount = total - total * maxDiscountPercent / 100;
  	double discountTotal = getDiscountFromPerson(orderLines.getDiscountTotal());
  	return (maxDiscount < discountTotal)? discountTotal:maxDiscount;
  }
  
  
  /**
   * @param totalFromOrderLines - the total made from all (discounted)
   *  subtotals in the order lines.
   * @return
   */
  private double getDiscountFromPerson(double totalFromOrderLines)
  {
  	return person.getDiscounts().subTotal(totalFromOrderLines,1);
  }

  
}
